package com.sat.wisdom.Fragments;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.sat.wisdom.R;
import com.sat.wisdom.Views.Bases.BasePage;
import com.sat.wisdom.Views.Impl.GovAffairsPage;
import com.sat.wisdom.Views.Impl.HomePage;
import com.sat.wisdom.Views.Impl.NewsCenterPage;
import com.sat.wisdom.Views.Impl.SettingPage;
import com.sat.wisdom.Views.Impl.SmartServicePage;
import com.sat.wisdom.Views.NoScrollViewPager;

import java.util.ArrayList;

/**
 * Project:Wisdom
 * Package:com.sat.wisdom.Fragments
 * Created by 透野君爱打奶炮 on 2018-04-25 at 17:00
 */
public class ContentFragment extends BaseFragment {

    private ArrayList<BasePage> mPages;
    private NoScrollViewPager vp_content;
    private RadioGroup rg_bottom_tab;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_content, null);
        //vp_content = ViewPager
        vp_content = view.findViewById(R.id.vp_content);
        rg_bottom_tab = view.findViewById(R.id.rg_bottom_tab);

        return view;
    }

    @Override
    public void initData() {
        mPages = new ArrayList<>();
        mPages.add(new HomePage(mActivity));
        mPages.add(new NewsCenterPage(mActivity));
        mPages.add(new SmartServicePage(mActivity));
        mPages.add(new GovAffairsPage(mActivity));
        mPages.add(new SettingPage(mActivity));

        vp_content.setAdapter(new ContentAdapter());

        rg_bottom_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bt_home:
                        //首页
                        vp_content.setCurrentItem(0,false);
                        break;
                    case R.id.bt_news:
                        //新闻中心
                        vp_content.setCurrentItem(1,false);
                        break;
                    case R.id.bt_smart:
                        //智慧
                        vp_content.setCurrentItem(2,false);
                        break;
                    case R.id.bt_gov:
                        //政务
                        vp_content.setCurrentItem(3,false);
                        break;
                    case R.id.bt_setting:
                        //设置
                        vp_content.setCurrentItem(4,false);
                        break;
                }
            }
        });

        //addOnPageChangeListener
        vp_content.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                BasePage page = mPages.get(position);
                page.initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mPages.get(0).initData();
    }

    public NewsCenterPage getNewsCenterPager() {
        NewsCenterPage newsCenterPage = (NewsCenterPage) mPages.get(1);
        return newsCenterPage;
    }

    class ContentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePage page = mPages.get(position);
            View view = page.mRootView; //获取当前page的布局
            //page.initData();
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
